<template>
    <el-dialog :title="props.title" v-model="upload.open" width="400px" append-to-body destroy-on-close>
        <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
            :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
            :auto-upload="false" drag>
            <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <template #tip>
                <div class="el-upload__tip text-center">
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                        @click="importTemplates">下载模板</el-link>
                </div>
            </template>
        </el-upload>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup name="ExcelUpload">
import { reactive, ref } from 'vue';
import { getToken } from "@/utils/auth";
const { proxy } = getCurrentInstance();
const upload = reactive({
    modelValue: [String, Object, Array],
    // 是否显示弹出层（用户导入）
    open: false,
    // 是否禁用上传
    isUploading: false,
    // 设置上传的请求头部
    headers: { Authorization: "Bearer " + getToken() },
    // 上传的地址
    url: import.meta.env.VITE_APP_BASE_API + "/system/user/importData"
});
const emit = defineEmits();
const props = defineProps({
    modelValue: [String, Object, Array],
    title: {
        type: String,
        default: '上传',
    },
    type: {
        type: String,
        default: '0',
    },
    //模板相关参数
    templateParams: {
        type: Object,
        default: {
            templateName: "模板名称",
            templateUrl: "模板下载地址",
            templateType: "0",//模板类型 0 跟随项目地址拼接 1.可以直接使用的地址
            templateObj: {},//下载模板时候的传参 默认为空
        }
    }
})
// 关闭
const close = () => {
    upload.open = false;
}
//开启
const open = (url) => {
    upload.url = import.meta.env.VITE_APP_BASE_API + url;
    upload.open = true;
}

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
    upload.isUploading = true;
};
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
    let { code, data, msg } = response;
    if (props.type == '0') {
        if (code == 200) {
            emit("update:modelValue", data);
            proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        } else {
            proxy.$message.warning(msg)
        }
    } else {
        if (code == 200) {
            emit("update:modelValue", data.sucess);
            if (data.error.length > 0) {
                let msg = data.error.join(',')
                proxy.$message.warning(msg)
            }
        } else {
            proxy.$message.warning(msg)
        }
    }

    upload.open = false;
    upload.isUploading = false;
    proxy.$refs["uploadRef"].handleRemove(file);

};
const importTemplates = () => {
    if (props.templateParams.templateType == '0') {
        proxy.download(props.templateParams.templateUrl, {...props.templateParams.templateObj}, `${props.templateParams.templateName}.xlsx`);
    } else {
        window.open(props.templateParams.templateUrl)
    }

}

/** 提交上传文件 */
function submitFileForm() {
    proxy.$refs["uploadRef"].submit();
};
//暴露方法给外部使用
defineExpose({
    open
});
</script>